package com.tarzan.edu.controller;


import com.tarzan.common.core.base.BaseResponse;
import com.tarzan.common.core.exception.BusinessException;
import com.tarzan.edu.entity.EduChapter;
import com.tarzan.edu.entity.response.ChapterVo;
import com.tarzan.edu.entity.response.EduChapterResp;
import com.tarzan.edu.service.IEduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("edu/chapter")
public class EduChapterController {



    @Autowired
    private IEduChapterService iEduChapterService;



    /** @title insert
     * @description TODO 新增章节
     * @param eduChapter
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @author liujinshan
     * @data 2021/5/21 15:52
     */
    @PostMapping(value = "insert",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse insert(@RequestBody EduChapter eduChapter){
        try{
            iEduChapterService.insert(eduChapter);
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }

    /** @title update
     * @description TODO 修改章节信息
     * @param eduChapter
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @author liujinshan
     * @data 2021/5/21 15:59
     */
    @PutMapping(value = "update",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse update(@RequestBody EduChapter eduChapter){
        try{
            iEduChapterService.update(eduChapter);
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }

    /** @title delete
     * @description TODO 删除章节信息
     * @param id
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @author liujinshan
     * @data 2021/5/21 16:01
     */
    @DeleteMapping(value = "delete/{id}")
    public BaseResponse delete(@PathVariable String id){
        try{
            iEduChapterService.delete(id);
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }


    /**
     * @description TODO 根据id查询章节相细信息
     * @author liujinshan
     * param id
     * @return com.tarzan.edu.entity.response.EduChapterResp
     * @throws
     * @date 2021-5-23 14:51
     */
    @GetMapping(value = "findTeacherById/{id}")
    public BaseResponse<EduChapterResp> findTeacherById(@PathVariable String id){
        BaseResponse<EduChapterResp> response;
        try{
            response = BaseResponse.ok(iEduChapterService.findTeacherById(id));
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return response;
    }


    //课程大纲列表,根据课程id进行查询
    @GetMapping("getChapterVideo/{courseId}")
    public BaseResponse<List<ChapterVo>>  getChapterVideo(@PathVariable String courseId) {
        BaseResponse<List<ChapterVo>> response;
        try{
            response = BaseResponse.ok(iEduChapterService.getChapterVideoByCourseId(courseId));
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return response;
    }

}
